package main

import (
	"fmt"
	"math/rand"
	"strconv"
	"sync"
	"time"
)

var (
	NameChan = make(chan string, 100)
	//信号量，控制每次只有5个人
	SemaphoreChan = make(chan bool, 5)
	//等待
	wg sync.WaitGroup
)

func GenerateName() {
	for i := 0; i < 100; i++ {
		NameChan <- "学员"+ strconv.Itoa(i+1)
	}
	close(NameChan)
}

func main() {
	GenerateName()
	for name := range NameChan {
		wg.Add(1)
		go Examming(name)
	}
	wg.Wait()
}

func Examming(name string) {
	SemaphoreChan <- true
	fmt.Printf("%s 正在考试...\n", name)
	time.Sleep(time.Millisecond * 200)
	rand.Seed(time.Now().UnixNano())
	num := rand.Intn(100)
	if num < 10{
		fmt.Printf("%s 考试作弊！退出考试\n", name)
	}
	<- SemaphoreChan
	fmt.Printf("%s 考试完毕！\n", name)
	wg.Done()
}
